﻿using System;
using System.Collections.Generic;
using Krilib;
using Krilib.Control;
using Krilib.Data;
using Krilib.Plugin;
using Krilib.Plugin.Operations;
using Krilib.Plugin.Operations.AgentElements;

namespace Wassrile.Operation
{
    public class Operation : Krilib.Plugin.Operation, IAgent
    {
        IPlugin plugin = null;
        readonly Service provideService;
        readonly StatusControl statusControl;

        public Operation(IPlugin parent)
        {
            plugin = parent;
            provideService = new DataClasses.WassrService();
            statusControl = new StatusControl();
        }

        public override void Begin()
        {
            //Initialie
            Core.InitCore(plugin as Plugin);
            Core.Config = Config.Config.Restore();
            //Set main datas
            Bridge.ControlManager.RegistInputMethod(new Controls.WInput());
            //Bridge.MainForm.AddCustomContextMenuCandidate();

            if (Core.Config.FirstBoot)
            {
                using (var ac = new Dialogs.AccountReq())
                {
                    if (ac.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        Core.Config.account.UserName = ac.UserId;
                        Core.Config.account.CredentialType = Wassrile.Config.Account.CredentialTypes.Token;
                        if (!ac.UseToken) Core.Config.account.CredentialType = Wassrile.Config.Account.CredentialTypes.BASIC;
                        Core.Config.account.PasswordOrToken = ac.Password;
                    }
                }
            }

            Action a = new Action(statusControl.Receiver.InitialReceive);
            a.BeginInvoke(new AsyncCallback((r) => { ((Action)r.AsyncState).EndInvoke(r); }), a);
        }

        public override void Exit()
        {
            Core.Config.Save();
        }

        public override Dictionary<string, ConfigPage> GetConfigPages()
        {
            var dic = new Dictionary<string, ConfigPage>();
            dic.Add("アカウントと受信", new Config.ConfigPage.AccountRecv());
            dic.Add("投稿と動作", new Config.ConfigPage.PostBehavior());
            dic.Add("詳細", new Config.ConfigPage.Detail());
            return dic;
        }

        public override Krilib.Plugin.Operation.OperationTypes OperationType
        {
            get { return OperationTypes.Agent; }
        }

        public override string ParentPluginId
        {
            get { return Core.Plugin.Id; }
        }

        public Service HostingService
        {
            get { return provideService; }
        }

        public string InputMethodId
        {
            get { return Define.InputMethodId; }
        }

        public IStatusControl Status
        {
            get { return statusControl; }
        }

        public string GetMasterId()
        {
            return Core.Config.account.UserName;
        }
    }
}